package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.ReceiveNewPalsAdapter;
import com.xinxin.facelinker.domain.ReceiveNewPals;
import com.xinxin.facelinker.domain.ShowPalsMotion;
import com.xinxin.facelinker.utils.NetHelper;

import java.util.ArrayList;
import java.util.List;

public class ReceiveNewPalsActivity extends Activity {

    private ListView lvReceiveNewPals;
    private String my_account_num;
    private ReceiveNewPalsAdapter adapter;
    private HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private List<ReceiveNewPals> list=new ArrayList<ReceiveNewPals>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_new_pals);

        lvReceiveNewPals = (ListView) findViewById(R.id.lvReceiveNewPals);
        lvReceiveNewPals.setAdapter(adapter);
        my_account_num = Config.getCachedAccountNum(ReceiveNewPalsActivity.this);
        btnBack=findViewById(R.id.btnBack);

        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_RECEIVE_NEW_PALS);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        final ProgressDialog pd = ProgressDialog.show(ReceiveNewPalsActivity.this, getResources().getString(R.string.receive_new_pals_connecting), getResources().getString(R.string.receive_new_pals_connecting_to_server));
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                pd.dismiss();
                Gson gson = new Gson();
                list= gson.fromJson(responseInfo.result, new TypeToken<List<ReceiveNewPals>>() {
                }.getType());

                adapter=new ReceiveNewPalsAdapter(list,ReceiveNewPalsActivity.this);
                lvReceiveNewPals.setAdapter(adapter);

                lvReceiveNewPals.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ReceiveNewPals receiveNewPals=list.get(position);
                        Intent intent=new Intent(ReceiveNewPalsActivity.this,ReceiveNewPalInfoActivity.class);
                        intent.putExtra(Config.KEY_OTHER_ACCOUNT_NUM,receiveNewPals.getAccount_num());
                        intent.putExtra(Config.KEY_PHOTO_URL,receiveNewPals.getPhoto_url());
                        intent.putExtra(Config.KEY_GENDER,receiveNewPals.getGender());
                        intent.putExtra(Config.KEY_RECEIVE_NEW_PAL_MESSAGE,receiveNewPals.getMessage());
                        startActivity(intent);
                    }
                });

//                lvReceiveNewPals.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//                    @Override
//                    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
//                        other_account_num = receiveNewPals.getOther_account_num()[position];
//                        RequestParams params = new RequestParams();
//                        params.addBodyParameter(Config.ACTION, Config.ACTION_DELETE_RECEIVE_NEW_PALS);
//                        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
//                        params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, other_account_num);
//                        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
//                            @Override
//                            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                                adapter.remove(lvReceiveNewPals.getItemAtPosition(position));
//                                adapter.notifyDataSetChanged();
//                                lvReceiveNewPals.invalidate();
//                                Toast.makeText(ReceiveNewPalsActivity.this, R.string.delete_blacklist_success,Toast.LENGTH_SHORT).show();
//
//                            }
//
//                            @Override
//                            public void onFailure(HttpException e, String s) {
//                                Toast.makeText(ReceiveNewPalsActivity.this, R.string.delete_blacklist_fail, Toast.LENGTH_SHORT).show();
//
//                            }
//                        });
//                        return false;
//                    }
//                });

            }

            @Override
            public void onFailure(HttpException e, String s) {
                pd.dismiss();
                Toast.makeText(ReceiveNewPalsActivity.this,R.string.receive_new_pals_fail,Toast.LENGTH_SHORT).show();
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
